package com.young4j.xinsuiwutuobang.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.young4j.xinsuiwutuobang.model.Customer;
import com.young4j.xinsuiwutuobang.service.CustomerService;
@WebServlet("/customer")
public class CustomerServlet extends HttpServlet {
	private CustomerService customerService;
	/**
	 * 在Servlet的init方法中进行对象的初始化。这样可以避免创建多个CustomerService实例，
	 * 其实整个Web应用中只需要一个CustomerService实例
	 */
	@Override
	public void init() throws ServletException {
		customerService = new CustomerService();
	}
	/**
	 * 将list对象放入请求属性中，最后通过请求对象重定向到customer.jsp视图
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<Customer> customerList = customerService.getCustomerList();
		req.setAttribute("customerList", customerList);
		req.getRequestDispatcher("/WEB-INF/view/customer.jsp").forward(req, resp);
	}
}
